//
//  ViewController.m
//  NativeDemo
//
//  Created by hibo on 2019/7/19.
//  Copyright © 2019 hibo. All rights reserved.
//

#import "ViewController.h"
#import <Flutter/Flutter.h>

@interface ViewController ()
@property (nonatomic,strong) FlutterViewController *vc;
@property (nonatomic,strong) FlutterBasicMessageChannel *msgChannel;
@property (nonatomic,strong) FlutterMethodChannel *methodChannel;
@end

@implementation ViewController

//1、初始化Flutter控制器
-(FlutterViewController *)vc{
    if (_vc == nil) {
        _vc = [[FlutterViewController alloc] init];
    }
    return _vc;
}
//2、FlutterMethodChannel像Flutter发送消息，Flutter端name需要保持一致
-(FlutterMethodChannel *)methodChannel{
    if (_methodChannel == nil) {
        _methodChannel = [FlutterMethodChannel methodChannelWithName:@"page_method" binaryMessenger:self.vc];
    }
    return _methodChannel;
}
//3、FlutterBasicMessageChannel接收消息 name 和flutter保持一致
-(FlutterBasicMessageChannel *)msgChannel{
    if (_msgChannel == nil) {
        _msgChannel = [FlutterBasicMessageChannel messageChannelWithName:@"messageChannel" binaryMessenger:self.vc];
            __weak typeof(self) weakSelf = self;
            //接收消息
            [_msgChannel setMessageHandler:^(id  _Nullable message, FlutterReply  _Nonnull callback) {
                NSLog(@"接收flutter发送过来的消息：%@",message);
                //判断是否为索要页面 进入初始页面
                if ([message isEqualToString:@"page?"]) {
                    [weakSelf.methodChannel invokeMethod:@"other" arguments:@"arg"];//方法名+参数
                }
            }];
    }
    return _msgChannel;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //向flutter发送消息
    [self.msgChannel sendMessage:@"I am pitt"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"混合开发";
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((width-200)/2, 200, 200, 40)];
    [button setTitle:@"进入第一个页面" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor grayColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [[UIButton alloc] initWithFrame:CGRectMake((width-200)/2, 280, 200, 40)];
    [button setTitle:@"进入第二个页面" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor grayColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    
    //初始化一个页面
    self.vc.title = @"初始页面";
    [self.msgChannel sendMessage:@"初始页面"];
    [self.navigationController pushViewController:self.vc animated:YES];
}
-(void)btn:(UIButton *)button{
    self.vc.title = button.titleLabel.text;
    //FlutterMethodChannel方法调用
    if ([button.titleLabel.text isEqualToString:@"进入第一个页面"]) {
        [self.methodChannel invokeMethod:@"one" arguments:@"arg"];//方法名+参数
    }else if ([button.titleLabel.text isEqualToString:@"进入第二个页面"]) {
        [self.methodChannel invokeMethod:@"two" arguments:@"arg"];//方法名+参数
    }else{
        [self.methodChannel invokeMethod:@"other" arguments:@"arg"];//方法名+参数
    }
    //监听Flutter调用
    __weak typeof(self) weakSelf = self;
    [self.methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        NSLog(@"method:%@",call.method);
        NSLog(@"arguments:%@",call.arguments);
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.navigationController pushViewController:self.vc animated:YES];
}


@end
